package com.meihua.shellapp.adapter.personalcenter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;
import com.meihua.shellapp.R;
import com.meihua.shellapp.base.BaseBean;
import com.meihua.shellapp.base.MyApplication;
import com.meihua.shellapp.bean.MyOrderBean;
import com.meihua.shellapp.dialog.BaseMessageDialog;
import com.meihua.shellapp.utils.DateUtils;
import com.meihua.shellapp.utils.ToastUtils;
import com.meihua.shellapp.utils.URL;

import java.util.List;

public class OrderFragmentAdapter extends RecyclerView.Adapter<OrderFragmentAdapter.OrderHandler> {
    private List<MyOrderBean.DataBean> mList;
    private Context mContext;
    private BaseMessageDialog dialog;

    public OrderFragmentAdapter(Context context, List<MyOrderBean.DataBean> list) {
        mContext = context;
        mList = list;
    }

    @Override
    public OrderHandler onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_order_fragment, null);
        // 实例化viewholder
        OrderHandler viewHolder = new OrderHandler(view);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(OrderHandler holder, final int position) {
        MyOrderBean.DataBean order = mList.get(position);
        holder.tv_order_name.setText("产品名称：" + order.getProduct_name());
        holder.tv_order_type.setText(order.getPay_status());
        holder.tv_order_num.setText("订单号：" + order.getOrder_num());
        holder.tv_bonded_size.setText("规模保费：￥" + order.getInsurance_money());
        holder.tv_buy_time.setText("购买时间：" + DateUtils.getInstance().stampToDate2(order.getCreate_time()));
        holder.tv_insurance_type.setText("类型：" + order.getInsurance_type());
        holder.tv_buy_person.setText("投保人：" + order.getPolicy_holder_name());
        holder.tv_del.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dialog = new BaseMessageDialog.Builder(mContext)
                        .setClickSure(new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                dialog.dismiss();
                                delOrder(position);
                            }
                        })
                        .setMessage("确定要删除吗？")
                        .show();
            }
        });
    }

    private void delOrder(final int position) {
        OkGo.<String>post(URL.getUrl().DELORDER)
                .params("order_num", mList.get(position).getOrder_num())
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        BaseBean bean = MyApplication.getInstance().getBeanForJson(BaseBean.class, response.body());
                        if (bean == null) {
                            return;
                        }
                        if (bean.getCode() == 1) {
                            mList.remove(position);
                            notifyDataSetChanged();
                        }
                        ToastUtils.getInstance().showToast(bean.getMsg());
                    }
                });
    }


    @Override
    public int getItemCount() {
        return mList.size();
    }


    public class OrderHandler extends RecyclerView.ViewHolder {
        TextView tv_order_name;
        TextView tv_order_type;
        TextView tv_order_num;
        TextView tv_bonded_size;
        TextView tv_buy_time;
        TextView tv_insurance_type;
        TextView tv_buy_person;
        TextView tv_del;

        public OrderHandler(View itemView) {
            super(itemView);
            tv_order_name = itemView.findViewById(R.id.tv_order_name);
            tv_order_type = itemView.findViewById(R.id.tv_order_type);
            tv_order_num = itemView.findViewById(R.id.tv_order_num);
            tv_bonded_size = itemView.findViewById(R.id.tv_bonded_size);
            tv_buy_time = itemView.findViewById(R.id.tv_buy_time);
            tv_insurance_type = itemView.findViewById(R.id.tv_insurance_type);
            tv_buy_person = itemView.findViewById(R.id.tv_buy_person);
            tv_del = itemView.findViewById(R.id.tv_del);
        }
    }
}